//package top.tenebrous.yzl.grpc;
//
//import com.alibaba.fastjson.JSON;
//import com.google.protobuf.Any;
//import io.grpc.stub.StreamObserver;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.server.service.GrpcService;
//import top.tenebrous.yzl.context.JesticContext;
//import top.tenebrous.yzl.context.JesticTask;
//import top.tenebrous.yzl.context.Response;
//import top.tenebrous.yzl.enums.FailCodeEnum;
//import top.tenebrous.yzl.enums.TaskEnum;
//import top.tenebrous.yzl.factorys.JesticPropertyChainFactory;
//import top.tenebrous.yzl.grpc.proto.common.GrpcResponse;
//import top.tenebrous.yzl.grpc.proto.common.RetCode;
//import top.tenebrous.yzl.grpc.proto.sechub.SechubRequest;
//import top.tenebrous.yzl.grpc.proto.sechub.SechubResponse;
//import top.tenebrous.yzl.grpc.proto.sechub.SechubServerGrpc;
//import top.tenebrous.yzl.model.request.JestcSechubRequst;
//import top.tenebrous.yzl.model.response.JestcSechubResponse;
//import top.tenebrous.yzl.utils.JesticContextInstance;
//
//import javax.annotation.Resource;
//import java.util.Objects;
//
///**
// * @author yazhang6
// */
//@Slf4j
//@GrpcService
//public class SechubServiceGrpcImpl extends SechubServerGrpc.SechubServerImplBase {
//
//    @Resource
//    private JesticPropertyChainFactory<? extends JesticTask, ? extends Response> jesticPropertyChainFactory;
//
//    /**
//     * 加密接口
//     * @param request
//     * @param responseObserver
//     */
//    @Override
//    public void encrypty(SechubRequest request, StreamObserver<GrpcResponse> responseObserver) {
//        JestcSechubRequst jestcSechubRequst = buildRequest(request, 1);
//        log.info("jestic [AuthorityServiceGrpcImpl.authority] server [{}] successfully established connection， request \n[{}]", jestcSechubRequst.getServerName(), JSON.toJSONString(jestcSechubRequst, true));
//
//        JesticContext<JestcSechubRequst, JestcSechubResponse> context = JesticContextInstance.initContext(jestcSechubRequst, new JestcSechubResponse(), TaskEnum.SECHUB);
//        jesticPropertyChainFactory.getHandler(context.getTaskEnum().getCode()).handler(context);
//        JestcSechubResponse contextResponse = context.getResponse();
//
//        if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_NOT_FOUND.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_NOT_FOUND.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_NOT_RUNNING.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_NOT_RUNNING.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_PARAM_NOT_FOUND.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_PARAM_NOT_FOUND.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_TOKEN_NOT_FOUND.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_TOKEN_NOT_FOUND.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else {
//            SechubResponse sechubResponse = SechubResponse.newBuilder().setData(contextResponse.getData()).build();
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetCode(RetCode.SUCCESS)
//                    .setRetMsg(RetCode.SUCCESS.name())
//                    .setData(Any.pack(sechubResponse))
//                    .build();
//            responseObserver.onNext(response);
//        }
//        responseObserver.onCompleted();
//    }
//
//    private JestcSechubRequst buildRequest(SechubRequest request, Integer type) {
//        JestcSechubRequst jestcSechubRequst = new JestcSechubRequst();
//        jestcSechubRequst.setServerId(request.getServerId());
//        jestcSechubRequst.setServerName(request.getServerName());
//        jestcSechubRequst.setInstanceId(request.getInstanceId());
//        jestcSechubRequst.setToken(request.getToken());
//        jestcSechubRequst.setPwd(request.getPwd());
//        jestcSechubRequst.setType(type);
//        return jestcSechubRequst;
//    }
//
//    /**
//     * 解密接口
//     * @param request
//     * @param responseObserver
//     */
//    @Override
//    public void decrypt(SechubRequest request, StreamObserver<GrpcResponse> responseObserver) {
//        JestcSechubRequst jestcSechubRequst = buildRequest(request, 2);
//    }
//}
